﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class LeveMarkForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public LeveMarkForm(Autodesk.Revit.DB.Document doc, int display)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			base.Location = new System.Drawing.Point(100, 100);
			this.initUI(doc, display);
		}

		public int GetDisplayElevation()
		{
			int result = 0;
			string a = "";
			foreach (object obj in this.groupBox1.Controls)
			{
				if (obj is RadioButton)
				{
					RadioButton radioButton = obj as RadioButton;
					if (radioButton.Checked)
					{
						a = radioButton.Text;
						break;
					}
				}
			}
			if (!(a == "选定高程"))
			{
				if (!(a == "顶部高程"))
				{
					if (!(a == "底部高程"))
					{
						if (a == "顶部和底部高程")
						{
							result = 3;
						}
					}
					else
					{
						result = 2;
					}
				}
				else
				{
					result = 1;
				}
			}
			else
			{
				result = 0;
			}
			return result;
		}

		private void LeveMarkForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				LeveMarkForm.SendMessage(LevelMark.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void LeveMarkForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			LeveMarkForm.SendMessage(LevelMark.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			LeveMarkForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			LeveMarkForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void LeveMarkForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void initUI(Autodesk.Revit.DB.Document doc, int display)
		{
			if (doc.ActiveView is View3D)
			{
				this.rbTop.Enabled = false;
				this.rbBtm.Enabled = false;
				this.rbTopB.Enabled = false;
				this.rbSel.Checked = true;
				return;
			}
			switch (display)
			{
			case 0:
				this.rbSel.Checked = true;
				this.rbTop.Checked = false;
				this.rbBtm.Checked = false;
				this.rbTopB.Checked = false;
				return;
			case 1:
				this.rbSel.Checked = false;
				this.rbTop.Checked = true;
				this.rbBtm.Checked = false;
				this.rbTopB.Checked = false;
				return;
			case 2:
				this.rbSel.Checked = false;
				this.rbTop.Checked = false;
				this.rbBtm.Checked = true;
				this.rbTopB.Checked = false;
				return;
			case 3:
				this.rbSel.Checked = false;
				this.rbTop.Checked = false;
				this.rbBtm.Checked = false;
				this.rbTopB.Checked = true;
				return;
			default:
				return;
			}
		}
	}
}
